import { ref, Ref, reactive } from "vue";
import axios from "axios";
import { config } from "../config";
interface Data {
    [keys: string]: string
}
interface Query {
    condition: string,
    limit: number,
    skip: number
}
//字符串拼接操作
export const getNewContent = function (DomElems: Array<{ value: string }>): Ref {
    let contentStr = ref('')
    if (DomElems) {
        for (let i = 0; i < DomElems.length; i++) {

            contentStr.value += ('<p>' + (DomElems[i].value as string) + '</p>\n')
        }
    }
    return contentStr
}

//文档响应式数据
export const reactDoc: { data: { res: Array<Data>, totalNum: number } } = reactive({
    data: {
        res: [],
        totalNum: 0
    }
})

//查询解析方法
export const getDocument = async (address: string, query: Query) => {

    reactDoc.data = (await axios.get(`${config.uri}/${address}?condition=${query.condition}&skip=${query.skip}&limit=${query.limit}`)).data

}

//重置方法
export const reset = (data: Data, condition?: { key: string, type: 'add' | 'stop' }[]) => {
    if (condition) {
        let key = []
        for (let i in data) {
            key.push(i)
        }
        for (let j of condition) {
            if (key.indexOf(j.key)) {
                if (j.type === 'add') {
                    data[j.key] = data[j.key].split('-')[0] + '-' + (parseInt(data[j.key].split('-')[1]) + 1).toString().padStart(3, '0')
                    key = key.filter(v => v != j.key)
                } else if (j.type === 'stop') {
                    key = key.filter(v => v != j.key)
                }

            }
        }

        for (let i of key) {
            data[i] = ''
        }
    } else {
        console.log('ok')
        console.log(data)
        for (let i in data) {
            data[i] = ''
        }
    }

}

interface State {
    subject: string,
    brief: string,
    docPage: string,
    content: string,
    prescription: string,
    [keys: string]: string
}

//响应式状态数据
export const state: State = reactive({
    subject: '',
    brief: '',
    docPage: '',
    content: '',
    prescription: '',

})

//保存在缓存
export const templateSave = (name: string, data: string | Data): void => {
    if (typeof data === 'object') {
        const dataStr = JSON.stringify(data)
        window.localStorage.setItem(name, dataStr)
        console.log(window.localStorage)
        alert('保存成功！')
    } else {
        window.localStorage.setItem(name, data)
        console.log(window.localStorage)
        alert('保存成功！')
    }
}

//上传数据库
export const upload = async (url: string/**网站带端口号和详细路径 */, data: Data/**对象，字段都是字符串 */) => {
    let flag = true
    if (data) {
        for (let i in data) {
            if (data[i] === '') {
                flag = false
                break
            }
        }
        if (flag) {
            const res = await axios.post(url, { ...data })
            console.log(res)
            alert('上传成功！')
            reset(data)

        } else {
            alert('数据不完整!')
        }
    }
}

export const getWeChatDoc = async (query: string, date:string) => {
    const res = await axios.get(`${config.uri}/api/weiChat?uri=${query}&date=${date}`)
    return res.data
}